/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VLSObjects;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import DBConnection.*;
import java.sql.*;

public class userManager {
    private Component frame;
    
    public boolean checkUser(String username, String password)
    {
        boolean result = false;

        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM userlist WHERE username = ? AND password = ? ");

            pstmt.setString(i++, username);
            pstmt.setString(i++, password);

            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result = true;
            }

            conn.close();
        }
        catch(SQLException e)
        {
           e.printStackTrace();
        }

        return result;

    }
    
    public boolean addUser(User newUser)
    {
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO userlist (username, password, surname, firstName, designation) VALUES(?,?,?,?,?)");

            //include parameters
            //pstmt.setString(i++, newUser.getUserID());
            pstmt.setString(i++, newUser.getUsername());
            pstmt.setString(i++, newUser.getPassword());
            pstmt.setString(i++, newUser.getSurname());
            pstmt.setString(i++, newUser.getFirstName());
            pstmt.setString(i++, newUser.getDesignation());

            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
    public boolean user_exist(String username, String userID)
    {
          boolean result = false;

        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM userlist WHERE username = ? AND userID != ?");

            pstmt.setString(i++, username);
            pstmt.setString(i++, userID);
            
            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result = true;
            }

            conn.close();
        }
        catch(SQLException e)
        {
           e.printStackTrace();
        }

        return result;
    }
    
    public boolean deleteUser(String username)
    {
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM userlist WHERE username = ?");

            //include parameters
            pstmt.setString(i++, username);

            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
    public boolean editUser(User editUser)
    {
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("UPDATE userlist SET username = ?, surname = ?, firstName = ?, designation = ? WHERE userID = ?");

            //include parameters
            pstmt.setString(i++, editUser.getUsername());
            pstmt.setString(i++, editUser.getSurname());
            pstmt.setString(i++, editUser.getFirstName());
            pstmt.setString(i++, editUser.getDesignation());
            pstmt.setString(i++, editUser.getUserID());
            

            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
    public boolean editPassword(String oldPassword, String newPassword, String userID)
    {
        boolean result = false;
        
        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM userlist WHERE userID = ? AND password = ?");

            pstmt.setString(i++, userID);
            pstmt.setString(i++, oldPassword);

            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result = true;
            }

            conn.close();
        }
                
        catch(SQLException e)
        {
           e.printStackTrace();
        }
        
        if(result == false) //if oldpassword input is incorrect
        {   
            ImageIcon notice_red = new ImageIcon("images/prompt_notice_red.png");
            JOptionPane.showMessageDialog(frame,
                                            "Incorrect Old Password!",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE,
                                            notice_red);
            
        }
        else if(result == true)//change password
        {
             try{
                //opens db connection
                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();

                int i = 1;

                //SQL Query
                PreparedStatement pstmt = conn.prepareStatement("UPDATE userlist SET password = ? WHERE userID = ?");

                //include parameters
                pstmt.setString(i++, newPassword);
                pstmt.setString(i++, userID);            

                //execute query
                pstmt.executeUpdate();

                //close DB connection
                conn.close();

                result=true;
                }
                catch(SQLException e)
                {
                   e.printStackTrace();
                }
             
                    ImageIcon notice_green = new ImageIcon("images/prompt_notice_green.png");
                    JOptionPane.showMessageDialog(frame,
                                            "Password Changed.",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE,
                                            notice_green);
         
        }
            return result;
    }
    
    public String designation(String username)
    {
          String result = "";

        try{
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT designation FROM userlist WHERE username = ?");

            pstmt.setString(i++, username);

            ResultSet rs = pstmt.executeQuery();

            while(rs.next())
            {
                result = rs.getString(1);
                
            }

            conn.close();
        }
        catch(SQLException e)
        {
           e.printStackTrace();
        }

        return result;
    }
    
}


